package binary_tree

type Employee struct {
	happy int
	nexts []*Employee
}

func MaxHappy(boss *Employee) int {
	if boss == nil {return 0}
	return MaxHappyProcess(boss, false)
}

func MaxHappyProcess(emp *Employee, isCome bool) int {
	if isCome {
		ans := 0
		for _, next := range emp.nexts {
			ans += MaxHappyProcess(next, false)
		}
		return ans
	} else {
		p1 := emp.happy
		p2 := 0
		for _, next := range emp.nexts {
			p1 += MaxHappyProcess(next, true)
			p2 += MaxHappyProcess(next, false)
		}
		return max(p1, p2)
	}
}

type MaxHappyInfo struct {
	yes int
	no  int

}

func MaxHappy1(boss *Employee) int {
	if boss == nil {return 0}
	info := MaxHappyProcess1(boss)
	return max(info.yes, info.no)
}

func MaxHappyProcess1(emp *Employee) MaxHappyInfo {
	if emp == nil {
		return MaxHappyInfo{0,0}
	}
	var curNo   int
	curYes := emp.happy
	for _, next := range emp.nexts {
		info := MaxHappyProcess1(next)
		curNo += max(info.yes, info.no)
		curYes += info.no
	}

	return MaxHappyInfo{curYes, curNo}
}

